//
//  ZCTextView.m
//  eXiaoTong
//
//  Created by ZhangCheng on 15/11/23.
//  Copyright © 2015年 ZhangCheng. All rights reserved.
//

#import "ZCTextView.h"

@implementation ZCTextView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void) awakeFromNib{
    [super awakeFromNib];
    [self setup];
}
- (void)setup
{
    self.toolbar = [[UIToolbar alloc] init];
    self.toolbar.frame = CGRectMake(0, 0, self.window.frame.size.width, 44);
    // set style
    [self.toolbar setBarStyle:UIBarStyleDefault];
    
    self.previousBarButton = [[UIBarButtonItem alloc] initWithTitle:@"上一项" style:UIBarButtonItemStylePlain target:self action:@selector(previousButtonIsClicked:)];
    self.nextBarButton = [[UIBarButtonItem alloc] initWithTitle:@"下一项" style:UIBarButtonItemStylePlain target:self action:@selector(nextButtonIsClicked:)];
    self.previousBarButton.enabled=self.preTextField!=nil;
    self.nextBarButton.enabled=self.nextTextField!=nil;
    
    UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonIsClicked:)];
    
    NSArray *barButtonItems = @[self.previousBarButton, self.nextBarButton, flexBarButton, doneBarButton];
    
    self.toolbar.items = barButtonItems;
    
    [self setInputAccessoryView:self.toolbar];
    
    //    _isTableView=[self.superview isKindOfClass:[UITableView class]];
    
}


- (void) doneButtonIsClicked:(id)sender
{
    
    [self resignFirstResponder];
    
}
- (void) nextButtonIsClicked:(id)sender
{
    if ([self.nextTextField isKindOfClass:[UITextField class]] || [self.nextTextField isKindOfClass:[UITextView class]]) {
        [self.nextTextField becomeFirstResponder];
    }
}

- (void) previousButtonIsClicked:(id)sender
{
    if ([self.preTextField isKindOfClass:[UITextField class]] || [self.preTextField isKindOfClass:[UITextView class]]) {
        [self.preTextField becomeFirstResponder];
    }
}
@end
